package com.eddie.libray.service;

import com.eddie.libray.LibraryDataSource;
import com.eddie.libray.entity.UserEntity;

import java.util.Random;
import java.util.Scanner;

/**
 * 注册业务处理
 *
 * @author Eddie
 * @since 2022/09/02 11:28
 **/
public class RegisterService {

    private final Scanner scanner = new Scanner(System.in);

    /**
     * 用户的注册
     */
    public void registerUser() {
        UserEntity userEntity = new UserEntity();
        System.out.println("请输入您的用户名：");
        userEntity.setName(scanner.next());
        //定义借书证号为随机的六位数
        userEntity.setLibraryCardCode(createCode());
        //这里直接在维护的用户列表中添加进去刚注册的用户
        LibraryDataSource.users.put(userEntity.getName(),userEntity);
        //就不设置登陆信息了，用户注册完成之后自己登录
        System.out.println("注册成功！，您的借书证号是：" + userEntity.getLibraryCardCode());
    }


    /**
     * 借书证号生成
     */
    private String createCode() {
        StringBuilder stringBuilder = new StringBuilder();
        //循环6次，每次生成一个1~10之间的数，使用stringBuilder将他们拼接并返回
        for (int i = 0; i < 6; i++) {
            stringBuilder.append(new Random().nextInt(10));
        }
        return stringBuilder.toString();
    }
}
